﻿function GetIndexService() {
    try {
        var currentWindow = Titanium.UI.getCurrentWindow();
        return desktopClient;
    }
    catch (exp) {
        return webClient;
    }
}
var desktopClient = {
    newWindow: function (url) {
        var screenWidth = document.documentElement.clientWidth;
        var screenHeight = document.documentElement.clientHeight;

        var desireWidth = 1024;
        var desireheight = 768;

        var desireX = (screenWidth - desireWidth) / 2 - 100 + ((Math.random() > 0.5) ? 15 : -15);
        var desireY = (screenHeight - desireheight) / 2 - 100 + ((Math.random() > 0.5) ? 15 : -15);

        var newWindow = Titanium.UI.createWindow({
            id: "propertyWindow",
            url: "app://FunctionPageHost.html?url=" + url,
            title: "My New Window",
            x: desireX,
            y: desireY,
            width: desireWidth,
            minWidth: 480,
            height: desireheight,
            minHeight: 360,
            maximizable: true,
            minimizable: true,
            closeable: true,
            resizable: true,
            fullscreen: false,
            maximized: false,
            minimized: false,
            usingChrome: true,
            topMost: false,
            visible: true,
            transparentBackground: false,
            transparency: false
        });

        newWindow.open();
        newWindow.show();
    },
    setTopMost: function (value) {
        Titanium.UI.getCurrentWindow().setTopMost(true);
    },
    createTray: function () { 
      var tray = Titanium.UI.addTray('app://style/images/NotifyIcon.ico');
            var menu = Titanium.UI.createMenu();

            //menu.appendItem(Titanium.UI.createMenuItem('设置'));
            menu.appendItem(Titanium.UI.createMenuItem('退出', function () { Titanium.App.exit(); }));

            tray.setMenu(menu);
            tray.setHint("易得桌面");
            return tray;  
    },
};

var webClient = {
    newWindow: function (url) {
        var screenWidth = document.documentElement.clientWidth;
        var screenHeight = document.documentElement.clientHeight;

        var desireWidth = 1024;
        var desireheight = 768;

        var desireX = (screenWidth - desireWidth) / 2 - 100 + ((Math.random() > 0.5) ? 15 : -15);
        var desireY = (screenHeight - desireheight) / 2 - 100 + ((Math.random() > 0.5) ? 15 : -15);

        var iframe = document.createElement('iframe');
                iframe.src = url;
                iframe.width = desireWidth;
                iframe.height = desireheight;
                iframe.frameborder = 'no';
                iframe.border = '0px;';
                $('body').append(iframe);
                $(iframe).css({ 'position': 'absolute', 'float': 'left', 'top': desireY, 'left': desireX });
    },
    setTopMost: function (value) {
        return;
    },
    createTray: function () { 
      return;
    },
};